// antd 按需加载
import { Toast, Portal } from '@ant-design/react-native';

import store from '../../../store/store.js'
import * as ActionTypes from '../../../constants/constants.js'
import { isUser, isWxUser, path, saveData, loadData, removeData, requstPromise,requestMessage } from '../../../util/utils.js'
import request from '../../../api/request.js'



export async function getDefaultAddressList(options) {
    const res = await request.MemberReceiveAddressController.list(options)
    let defaultAddress
    console.log(res)
    res.data.data.map((item) => {
        if (item.defaultStatus == 1) {
            defaultAddress = item
        }
    })
    store.dispatch({
        type: ActionTypes.defaultAddress,
        data: defaultAddress
    })
}

export async function defaultAddress(options) {
    const loading = Toast.loading('加载中', 0, null, false)
    const res = await request.MemberReceiveAddressController.default(options)
    Portal.remove(loading)
    console.log(res)
    return requestMessage(res)
}

export async function getAddressList(options) {
    const { dispatch } = this.props
    // const { refresh } = options
    const loading = Toast.loading('加载中', 0, null, false)
    const res = await request.MemberReceiveAddressController.list(options)
    Portal.remove(loading)
    if (res.error) {
        dispatch({
            type: ActionTypes.addressListError,
            data: res
        })
        return
    }
    // dispatch({
    //     type: ActionTypes.addressListRefresh,
    //     data: res.data.data
    // })
    dispatch({
        type: ActionTypes.addressList,
        data: res.data.data
    })
}

export async function setDefaultAddress(options) {
    const { dispatch } = this.props
    const loading = Toast.loading('加载中', 0, null, false)
    const res = await request.MemberReceiveAddressController.update(options)
    Portal.remove(loading)
    if (res.data.code == 200) {
        Toast.success('设置成功', 1, null, false)
        Actions.pop()
    } else {
        Toast.fail('设置失败', 1, null, false)
    }
}


export async function deleteAddress(options) {
    const { dispatch } = this.props
    const loading = Toast.loading('加载中', 0, null, false)
    const res = await request.MemberReceiveAddressController.delete(options)
    Portal.remove(loading)
    if (res.data.code == 200) {
        Toast.success('删除成功', 1, null, false)
        Actions.pop()
    } else {
        Toast.fail('删除失败', 1, null, false)
    }
    return requstPromise(res)
}


